package com.shangcheng.server.controller;




import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import com.shangcheng.commons.api.CommonResult;
import com.shangcheng.model.domain.UmsResourceCategory;
import com.shangcheng.model.vo.result.Result;
import com.shangcheng.server.service.UmsResourceCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 后台资源分类管理Controller
 */
@RestController
@RequestMapping("/resourceCategory")
public class UmsResourceCategoryController {
    @Autowired
    private UmsResourceCategoryService resourceCategoryService;

    /**
     * 添加后台资源分类
     * @param umsResourceCategory
     * @return
     */
    @PostMapping("/create")
    public ResponseEntity createResourceCategory(@RequestBody UmsResourceCategory umsResourceCategory) {
        umsResourceCategory.setCreateTime(new DateTime());
        resourceCategoryService.createResourceCategory(umsResourceCategory);
        return ResponseEntity.ok(new Result(200,null,"添加成功"));
    }

    /**
     * 根据id删除后台资源分类
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public ResponseEntity deleteResourceCategory(@PathVariable("id") Integer id){
        resourceCategoryService.deleteResourceCategory(id);
        return ResponseEntity.ok(new Result(200,null,"删除成功"));
    }

    /**
     * 查询所有后台资源分类
     * @return
     */
    @GetMapping("listAll")
    public ResponseEntity resourceCategoryList(){
        List<UmsResourceCategory> list= resourceCategoryService.resourceCategoryList();
        return ResponseEntity.ok(new Result(200,list,null));
    }

    /**
     * 修改后台资源分类
     * @param id
     * @param umsResourceCategory
     * @return
     */
    @PostMapping("/update/{id}")
    public ResponseEntity updateResourceCategory(@PathVariable("id") Integer id,@RequestBody UmsResourceCategory umsResourceCategory){
        umsResourceCategory.setId(Convert.toLong(id));
        resourceCategoryService.updateResourceCategory(umsResourceCategory);
        return ResponseEntity.ok(new Result(200,null,"修改成功"));
    }
}
